﻿using BCCommon;
using BCEntity.Common.SystemConfiguration;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCData.Common.SystemConfiguration
{
    /// <summary>
    /// 系统配置
    /// </summary>
    public class SystemConfigurationData : ISystemConfigurationData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM systemconfiguration";
        private const string QUERY = @"SELECT SystemConfigurationId,ListenCountdownRange,EnableHorsemanAcceptOrderCount,HorsemanAcceptOrderCount,EnableHorsemanTodayCancelOrderCount,
                                        HorsemanTodayCancelOrderCount,OrderCommissionRatio,EnableSubCompanyOrderCommissionRatio,EnableUserPointsRatio,UserPointsRatio,EnableUserGrowthRatio,
                                        MinProgramHeadColor,UserGrowthRatio,EnableListenCountdownPriorityType,ListenCountdownPriorityType,EnableDelayTakingForm,DelayTakingFormSeconds,EnableHideenCountdown,
                                        HideenCountdownSeconds,EnableHorsemanPickupDistanceLimit,HorsemanPickupDistanceLimit,EnableVehiclePickupDistanceLimit,MinProgramFontColor,VehiclePickupDistanceLimit,
                                        EnableInvoiceApplication,EnableFerryFee,FerryFee,EnableCashAdvanceMinAmount,CashAdvanceMinAmount,EnableCashAdvanceMaxTimes,CashAdvanceMaxTimes,EnableCashAdvanceTimesCycle,
                                        CashAdvanceTimesCycle,MinProgramTitle,EnableCashAdvanceTodayHourRangeLimit,CashAdvanceTodayStartHourLimit,CashAdvanceTodayEndHourLimit,EnableSecurityDeposit,
                                        SecurityDepositAmount,EnableUserRechargeGiftProportion,UserRechargeGiftProportion,EnableDriverQrcode,DriverAuthorizeBackgroundAddress,DriverShareImageAddress,
                                        ClientAppCopyright,DriverInviteSharePosterAddress,DriverLoginBackgroundAddress,DriverHomeBackgroundAddress,DriverQuickAcceptOrderIcon,DriverPendingQuickAcceptOrderIcon,
                                        DriverAcceptOrderSettingIcon,DriverAcceptOrderRecordIcon,ClientAuthorizeBackgroundAddress,ClientShareImageAddress,ClientInviteSharePosterAddress,CustomerPhone,
                                        ClientHomeHorsemanLocationIcon,ClientHomeDriverLocationIcon,ClientHomeRightDownButtonIcon,QrcodePosterAddress,OrderTransportingIcon,EnablePlatformSystem,
                                        ClosedPlatformRemark,WebsocketHostUrl,EnablePublicWelfareFundProportion,PublicWelfareFundProportion,EnablePickupCode,DriverAcceptOrderMusic,SimNewOrderMusic,
                                        EnablePlatformBusinessRange,PlatformBusinessStartHour,PlatformBusinessEndHour,EnableTimeWatermark,TimeWatermarkAddress,CreateTime,CreateAdminId,CreateAdminName,
                                        EnablePickupPhoto,UpdateTime,EditAdminId,EditAdminName,EnableListenCountdown 
                                    FROM systemconfiguration";
        public SystemConfigurationData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查系统配置是否存在
        public async Task<bool> Exists(int systemsConfigurationId)
        {
            string sql = string.Format(@"{0} WHERE SystemConfigurationId=?SystemConfigurationId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SystemConfigurationId", systemsConfigurationId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 修改系统配置
        public async Task<SystemConfigurationEntity> Update(SystemConfigurationEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("systemconfiguration", new string[] { "ListenCountdownRange", "EnableHorsemanAcceptOrderCount", "HorsemanAcceptOrderCount", "EnableHorsemanTodayCancelOrderCount",
                "HorsemanTodayCancelOrderCount", "OrderCommissionRatio", "EnableSubCompanyOrderCommissionRatio", "EnableUserPointsRatio", "UserPointsRatio", "EnableUserGrowthRatio", "MinProgramHeadColor", 
                "UserGrowthRatio", "EnableListenCountdownPriorityType", "ListenCountdownPriorityType", "EnableDelayTakingForm", "DelayTakingFormSeconds", "EnableHideenCountdown", "HideenCountdownSeconds", 
                "EnableHorsemanPickupDistanceLimit", "HorsemanPickupDistanceLimit", "EnableVehiclePickupDistanceLimit", "MinProgramFontColor", "VehiclePickupDistanceLimit", "EnableInvoiceApplication", 
                "EnableFerryFee", "FerryFee", "EnableCashAdvanceMinAmount", "CashAdvanceMinAmount", "EnableCashAdvanceMaxTimes", "CashAdvanceMaxTimes", "EnableCashAdvanceTimesCycle", "CashAdvanceTimesCycle",
                "MinProgramTitle", "EnableCashAdvanceTodayHourRangeLimit", "CashAdvanceTodayStartHourLimit", "CashAdvanceTodayEndHourLimit", "EnableSecurityDeposit", "SecurityDepositAmount",
                "EnableUserRechargeGiftProportion", "UserRechargeGiftProportion", "EnableDriverQrcode", "DriverAuthorizeBackgroundAddress", "DriverShareImageAddress", "ClientAppCopyright", 
                "DriverInviteSharePosterAddress", "DriverLoginBackgroundAddress", "DriverHomeBackgroundAddress", "DriverQuickAcceptOrderIcon", "DriverPendingQuickAcceptOrderIcon", "DriverAcceptOrderSettingIcon",
                "DriverAcceptOrderRecordIcon", "ClientAuthorizeBackgroundAddress", "ClientShareImageAddress", "ClientInviteSharePosterAddress", "CustomerPhone", "ClientHomeHorsemanLocationIcon", 
                "ClientHomeDriverLocationIcon", "ClientHomeRightDownButtonIcon", "QrcodePosterAddress", "OrderTransportingIcon", "EnablePlatformSystem", "ClosedPlatformRemark", "WebsocketHostUrl", 
                "EnablePublicWelfareFundProportion", "PublicWelfareFundProportion", "EnablePickupCode", "DriverAcceptOrderMusic", "SimNewOrderMusic", "EnablePlatformBusinessRange", "PlatformBusinessStartHour",
                "PlatformBusinessEndHour", "EnableTimeWatermark", "TimeWatermarkAddress", "CreateAdminId", "CreateAdminName", "EnablePickupPhoto", "UpdateTime", "EditAdminId", "EditAdminName",
                "EnableListenCountdown" }, new string[] { "SystemConfigurationId" });
            sql = string.Format("{0} {1} WHERE SystemConfigurationId=?SystemConfigurationId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SystemConfigurationId", entity.SystemConfigurationId, DbType.Int32, size: 11);
            parameters.Add("?ListenCountdownRange", entity.ListenCountdownRange, DbType.Int32, size: 11);
            parameters.Add("?EnableHorsemanAcceptOrderCount", entity.EnableHorsemanAcceptOrderCount, DbType.Boolean);
            parameters.Add("?HorsemanAcceptOrderCount", entity.HorsemanAcceptOrderCount, DbType.Int32, size: 11);
            parameters.Add("?EnableHorsemanTodayCancelOrderCount", entity.EnableHorsemanTodayCancelOrderCount, DbType.Boolean);
            parameters.Add("?HorsemanTodayCancelOrderCount", entity.HorsemanTodayCancelOrderCount, DbType.Int32, size: 11);
            parameters.Add("?OrderCommissionRatio", entity.OrderCommissionRatio, DbType.Decimal, size: 6);
            parameters.Add("?EnableSubCompanyOrderCommissionRatio", entity.EnableSubCompanyOrderCommissionRatio, DbType.Boolean);
            parameters.Add("?EnableUserPointsRatio", entity.EnableUserPointsRatio, DbType.Boolean);
            parameters.Add("?UserPointsRatio", entity.UserPointsRatio, DbType.Decimal, size: 6);
            parameters.Add("?EnableUserGrowthRatio", entity.EnableUserGrowthRatio, DbType.Boolean);
            parameters.Add("?MinProgramHeadColor", entity.MinProgramHeadColor?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?UserGrowthRatio", entity.UserGrowthRatio, DbType.Decimal, size: 6);
            parameters.Add("?EnableListenCountdownPriorityType", entity.EnableListenCountdownPriorityType, DbType.Boolean);
            parameters.Add("?ListenCountdownPriorityType", entity.ListenCountdownPriorityType.ToString(), DbType.AnsiString);
            parameters.Add("?EnableDelayTakingForm", entity.EnableDelayTakingForm, DbType.Boolean);
            parameters.Add("?DelayTakingFormSeconds", entity.DelayTakingFormSeconds, DbType.Int32, size: 11);
            parameters.Add("?EnableHideenCountdown", entity.EnableHideenCountdown, DbType.Boolean);
            parameters.Add("?HideenCountdownSeconds", entity.HideenCountdownSeconds, DbType.Int32, size: 11);
            parameters.Add("?EnableHorsemanPickupDistanceLimit", entity.EnableHorsemanPickupDistanceLimit, DbType.Boolean);
            parameters.Add("?HorsemanPickupDistanceLimit", entity.HorsemanPickupDistanceLimit, DbType.Int32, size: 11);
            parameters.Add("?EnableVehiclePickupDistanceLimit", entity.EnableVehiclePickupDistanceLimit, DbType.Boolean);
            parameters.Add("?MinProgramFontColor", entity.MinProgramFontColor?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?VehiclePickupDistanceLimit", entity.VehiclePickupDistanceLimit, DbType.Int32, size: 11);
            parameters.Add("?EnableInvoiceApplication", entity.EnableInvoiceApplication, DbType.Boolean);
            parameters.Add("?EnableFerryFee", entity.EnableFerryFee, DbType.Boolean);
            parameters.Add("?FerryFee", entity.FerryFee, DbType.Decimal, size: 6);
            parameters.Add("?EnableCashAdvanceMinAmount", entity.EnableCashAdvanceMinAmount, DbType.Boolean);
            parameters.Add("?CashAdvanceMinAmount", entity.CashAdvanceMinAmount, DbType.Decimal, size: 10);
            parameters.Add("?EnableCashAdvanceMaxTimes", entity.EnableCashAdvanceMaxTimes, DbType.Boolean);
            parameters.Add("?CashAdvanceMaxTimes", entity.CashAdvanceMaxTimes, DbType.Int32, size: 11);
            parameters.Add("?EnableCashAdvanceTimesCycle", entity.EnableCashAdvanceTimesCycle, DbType.Boolean);
            parameters.Add("?CashAdvanceTimesCycle", entity.CashAdvanceTimesCycle.ToString(), DbType.AnsiString);
            parameters.Add("?MinProgramTitle", entity.MinProgramTitle?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?EnableCashAdvanceTodayHourRangeLimit", entity.EnableCashAdvanceTodayHourRangeLimit, DbType.Boolean);
            parameters.Add("?CashAdvanceTodayStartHourLimit", entity.CashAdvanceTodayStartHourLimit, DbType.Int32, size: 11);
            parameters.Add("?CashAdvanceTodayEndHourLimit", entity.CashAdvanceTodayEndHourLimit, DbType.Int32, size: 11);
            parameters.Add("?EnableSecurityDeposit", entity.EnableSecurityDeposit, DbType.Boolean);
            parameters.Add("?SecurityDepositAmount", entity.SecurityDepositAmount, DbType.Decimal, size: 10);
            parameters.Add("?EnableUserRechargeGiftProportion", entity.EnableUserRechargeGiftProportion, DbType.Boolean);
            parameters.Add("?UserRechargeGiftProportion", entity.UserRechargeGiftProportion, DbType.Decimal, size: 10);
            parameters.Add("?EnableDriverQrcode", entity.EnableDriverQrcode, DbType.Boolean);
            parameters.Add("?DriverAuthorizeBackgroundAddress", entity.DriverAuthorizeBackgroundAddress?.Trim(), DbType.String);
            parameters.Add("?DriverShareImageAddress", entity.DriverShareImageAddress?.Trim(), DbType.String);
            parameters.Add("?ClientAppCopyright", entity.ClientAppCopyright?.Trim(), DbType.String);
            parameters.Add("?DriverInviteSharePosterAddress", entity.DriverInviteSharePosterAddress?.Trim(), DbType.String);
            parameters.Add("?DriverLoginBackgroundAddress", entity.DriverLoginBackgroundAddress?.Trim(), DbType.String);
            parameters.Add("?DriverHomeBackgroundAddress", entity.DriverHomeBackgroundAddress?.Trim(), DbType.String);
            parameters.Add("?DriverQuickAcceptOrderIcon", entity.DriverQuickAcceptOrderIcon?.Trim(), DbType.String);
            parameters.Add("?DriverPendingQuickAcceptOrderIcon", entity.DriverPendingQuickAcceptOrderIcon?.Trim(), DbType.String);
            parameters.Add("?DriverAcceptOrderSettingIcon", entity.DriverAcceptOrderSettingIcon?.Trim(), DbType.String);
            parameters.Add("?DriverAcceptOrderRecordIcon", entity.DriverAcceptOrderRecordIcon?.Trim(), DbType.String);
            parameters.Add("?ClientAuthorizeBackgroundAddress", entity.ClientAuthorizeBackgroundAddress?.Trim(), DbType.String);
            parameters.Add("?ClientShareImageAddress", entity.ClientShareImageAddress?.Trim(), DbType.String);
            parameters.Add("?ClientInviteSharePosterAddress", entity.ClientInviteSharePosterAddress?.Trim(), DbType.String);
            parameters.Add("?CustomerPhone", entity.CustomerPhone?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?ClientHomeHorsemanLocationIcon", entity.ClientHomeHorsemanLocationIcon?.Trim(), DbType.String);
            parameters.Add("?ClientHomeDriverLocationIcon", entity.ClientHomeDriverLocationIcon?.Trim(), DbType.String);
            parameters.Add("?ClientHomeRightDownButtonIcon", entity.ClientHomeRightDownButtonIcon?.Trim(), DbType.String);
            parameters.Add("?QrcodePosterAddress", entity.QrcodePosterAddress?.Trim(), DbType.String);
            parameters.Add("?OrderTransportingIcon", entity.OrderTransportingIcon?.Trim(), DbType.String);
            parameters.Add("?EnablePlatformSystem", entity.EnablePlatformSystem, DbType.Boolean);
            parameters.Add("?ClosedPlatformRemark", entity.ClosedPlatformRemark?.Trim(), DbType.String);
            parameters.Add("?WebsocketHostUrl", entity.WebsocketHostUrl?.Trim(), DbType.String);
            parameters.Add("?EnablePublicWelfareFundProportion", entity.EnablePublicWelfareFundProportion, DbType.Boolean);
            parameters.Add("?PublicWelfareFundProportion", entity.PublicWelfareFundProportion, DbType.Decimal, size: 10);
            parameters.Add("?EnablePickupCode", entity.EnablePickupCode, DbType.Boolean);
            parameters.Add("?DriverAcceptOrderMusic", entity.DriverAcceptOrderMusic?.Trim(), DbType.String);
            parameters.Add("?SimNewOrderMusic", entity.SimNewOrderMusic?.Trim(), DbType.String);
            parameters.Add("?EnablePlatformBusinessRange", entity.EnablePlatformBusinessRange, DbType.Boolean);
            parameters.Add("?PlatformBusinessStartHour", entity.PlatformBusinessStartHour, DbType.Int32, size: 11);
            parameters.Add("?PlatformBusinessEndHour", entity.PlatformBusinessEndHour, DbType.Int32, size: 11);
            parameters.Add("?EnableTimeWatermark", entity.EnableTimeWatermark, DbType.Boolean);
            parameters.Add("?TimeWatermarkAddress", entity.TimeWatermarkAddress?.Trim(), DbType.String);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?EnablePickupPhoto", entity.EnablePickupPhoto, DbType.Boolean);
            parameters.Add("?UpdateTime", entity.UpdateTime, DbType.DateTime);
            parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
            parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?EnableListenCountdown", entity.EnableListenCountdown, DbType.Boolean);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<SystemConfigurationEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 获取系统配置
        public async Task<SystemConfigurationEntity> GetEntity()
        {
            string sql = string.Format(@"{0} WHERE SystemConfigurationId=1", QUERY);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<SystemConfigurationEntity>(sql);
        }
        #endregion

    }
}
